
/* Generated by Interface Builder */

#import "PieView.h"
#import "stdio.h"
#import <dpsclient/wraps.h>
#import <appkit/Font.h>
#import <appkit/Cell.h>
#import <appkit/Matrix.h>
#import <appkit/Slider.h>
#import <appkit/OpenPanel.h>
#import <appkit/Panel.h>
#import <strings.h>
#import "slice.h"
#import "math.h"

@implementation PieView

+newFrame:(const NXRect *)tF
{
   int i;
   float c_x, c_y;  // X and Y center of view
   
   // we are using the Appkit "newFrame" plus our own additions
   self = [super newFrame:tF];
   
   nSlices = 9;
   
   for (i=0; i<=nSlices; i++)
    {
       // shuffle the grays so adjecent ones are different
       gray[i] = .1*(i*(i%2) + (10-i)*((i+1)%2));
       myFloat[i] = (float) (i+1)*5.0;
   }
   strcpy(myLabel[0],"ABC");
   strcpy(myLabel[1],"DEF");
   strcpy(myLabel[2],"GHI");
   strcpy(myLabel[3],"JKL");
   strcpy(myLabel[4],"MNO");
   strcpy(myLabel[5],"PQR");
   strcpy(myLabel[6],"STU");
   strcpy(myLabel[7],"VWX");
   strcpy(myLabel[8],"YZa");
   strcpy(myLabel[9],"bcd");
   
   myFont = [Font newFont:"Courier-Bold"
                  size:20.0
		  style:0
		  matrix:NX_IDENTITYMATRIX];
		  

   pieSize = 100.0;
   pieFontSize = 20.0;
   openReq = [OpenPanel new];
   c_x = bounds.size.width/2.0;
   c_y = bounds.size.height/2.0;
   [self translate:c_x :c_y];
   return self;
}

- getValue:sender
{
    int i;
    i = [sender selectedRow];
    myFloat[i] = [sender floatValue];
    // printf("Row = %d  Value = %f\n", i, myFloat[i]);
    [self display];
    return self;
}

- getLabel:sender
{
    int i;
    i = [sender selectedRow];
    strcpy(myLabel[i],[sender stringValue]);
    // printf("Row = %d  String = %s\n", i, myLabel[i]);
    [self display];
    return self;
}

- getnSlices:sender
{
    nSlices = [sender intValue];
    [self display];
    return self;
}

- getPieSize:sender
{
    pieSize = [sender floatValue];
    [self display];
    return self;
}

- getPieFontSize:sender
{
    pieFontSize = [sender floatValue];
    myFont = [Font newFont:"Courier-Bold"
                  size:pieFontSize
		  style:0
		  matrix:NX_IDENTITYMATRIX];
    [self display];
    return self;
}

- setValueMatrix:anObject
{
   int i;
   id myCell;
   valueMatrix = anObject;
   for (i=0; i<=nSlices; i++) {
      myCell = [valueMatrix cellAt:i:0];
      [myCell setFloatValue:myFloat[i]];
   }
   [valueMatrix display];
   return self;
}

- setLabelMatrix:anObject
{
   int i;
   id myCell;
   labelMatrix = anObject;
   for (i=0; i<=nSlices; i++) {
      myCell = [labelMatrix cellAt:i:0];
      [myCell setStringValue:myLabel[i]];
   }
   [labelMatrix display];
   return self;
}

- showError: (char *)errorMessage
{
  NXRunAlertPanel("Pie_Matrix", errorMessage,"OK",NULL,NULL);
}

- openRequest:sender
{
    const char *fileName;
    const char *const types[4] = {"data",NULL};
    int ok;

    if ([openReq runModalForTypes:types] && (fileName =[openReq filename])) {
	[self openFile:fileName];
    }
    else
	[self showError:"No file chosen or could not open file"];
    return self;
}

-(int) openFile:(const char *)fileName
{
  int i=0;
  char buf[MAX_CHARS_PER_LINE];
  FILE *input_fp;
  printf("Opening File = %s\n", fileName);
  
  if (( input_fp = fopen(fileName, "r") ) == NULL ) {
     [self showError:"Could not open file"];
     fprintf(stderr, "File: %s Could not be opened.\n", fileName);
     return NO;
  }
  
  while (fgets(buf, MAX_CHARS_PER_LABEL, input_fp) != NULL)
  {
     if (i >= MAX_SLICES) break;
     sscanf(buf, "%f %s", &myFloat[i], myLabel[i]);
     // printf("Value = %f  Label = %s\n", myFloat[i], myLabel[i]);
     i++;
  }
  printf("%d data points read\n", i);
  nSlices = i;
  fclose(input_fp);
  [self display];
  [self setLabelMatrix:labelMatrix];
  [self setValueMatrix:valueMatrix];
  return YES;

}


- drawSelf:(NXRect*)r :(int)c
{
    int i;
    float total = 0.0;
    [myFont set];
    PSsetgray(1.0);
    NXRectFill(r);
    PSsetgray(0.0);
    for (i=0; i<nSlices; i++)
    {
     drawSlice(gray[i], pieSize, total, total+myFloat[i], pieFontSize, myLabel[i]);
       total = total + myFloat[i];
    }
    return self;
}

@end
